package com.efast.dmom.mes.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.PartAlternate;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PartAlternateDataHandler;
import com.efast.dmom.mes.entity.PartAlternateEntity;
import com.efast.dmom.mes.service.PartAlternateService;
import com.efast.dmom.mes.service.PartService;

@Service
public class PartAlternateServiceImpl implements PartAlternateService{
	@Autowired
	private PartAlternateDataHandler partAlternateDataHandler;
	@Autowired
	private PartService partService;
	
	@Override
	public ResultBean save(PartAlternate partAlternate) {
		ResultBean resultBean = new ResultBean();
		if(partAlternate==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		
		resultBean = checkSavePartAlternateInfo(partAlternate);
		
		if(resultBean.isSuccess()) {
			return partAlternateDataHandler.save(partAlternate);
		}else {
			return resultBean;
		}
		
	}

	@Override
	public ResultBean save(List<PartAlternate> partAlternates) {
		ResultBean resultBean = new ResultBean();
		if(partAlternates==null || partAlternates.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		for(PartAlternate partAlternate:partAlternates) {
			resultBean = checkSavePartAlternateInfo(partAlternate);
			if(!resultBean.isSuccess()) {
				return resultBean;
			}
		}
		
		return partAlternateDataHandler.saveBatch(partAlternates);
	}

	@Override
	public ResultBean update(PartAlternate partAlternate) {
		ResultBean resultBean = new ResultBean();
		if(partAlternate==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		resultBean = checkUpdatePartAlternateInfo(partAlternate);
		if(!resultBean.isSuccess()) {
			return resultBean;
		}
		return partAlternateDataHandler.update(partAlternate);
	}

	@Override
	public ResultBean update(List<PartAlternate> partAlternates) {
		ResultBean resultBean = new ResultBean();
		if(partAlternates==null || partAlternates.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		for(PartAlternate partAlternate:partAlternates) {
			resultBean = checkUpdatePartAlternateInfo(partAlternate);
			if(!resultBean.isSuccess()) {
				return resultBean;
			}
		}
		return partAlternateDataHandler.updateBatch(partAlternates);
	}
	
	
	@Override
	public ResultBean delete(PartAlternate partAlternate) {
		ResultBean resultBean = new ResultBean();
		if(partAlternate==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		
		resultBean = checkDeletePartAlternateInfo(partAlternate);
		if(!resultBean.isSuccess()) {
			return resultBean;
		}
		return partAlternateDataHandler.delete(partAlternate);
	}

	@Override
	public ResultBean delete(List<PartAlternate> partAlternates) {
		ResultBean resultBean = new ResultBean();
		if(partAlternates==null || partAlternates.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		for(PartAlternate partAlternate:partAlternates) {
			resultBean = checkDeletePartAlternateInfo(partAlternate);
			if(!resultBean.isSuccess()) {
				return resultBean;
			}
		}
		return partAlternateDataHandler.deleteBatch(partAlternates);
	}

	@Override
	public PartAlternate getById(long id) {
		return partAlternateDataHandler.getById(id);
	}

	@Override
	public List<PartAlternate> getByPartId(long id) {
		return partAlternateDataHandler.getByPartId(id);
	}

	@Override
	public List<PartAlternate> getByAlternatePartId(long id) {
		return partAlternateDataHandler.getByAlternatePartId(id);
	}

	@Override
	public List<Part> getAlternatePartByPartId(long id) {
		return partAlternateDataHandler.getAlternatePartByPartId(id);
	}

	@Override
	public List<Part> getPartByAlternatePartId(long id) {
		return partAlternateDataHandler.getPartByAlternatePartId(id);
	}
	
	//check PartAlternate data and set backgroud data
	private ResultBean checkSavePartAlternateInfo(PartAlternate partAlternate) {
		
		ResultBean resultBean = new ResultBean();
		
		
		PartAlternateEntity entity = partAlternate.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		boolean flag = true;
		

//		private long partId;
//		private String partNo;
//		private String partRevision;
		long partId = entity.getPartId();
		Part part = partService.getById(partId);
		if(part == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partId:"+partId+" error");
			return resultBean;
		}
		if(flag) {
			entity.setPartNo(part.getInnerData().getPartNo());
			entity.setPartRevision(part.getInnerData().getRevision());
		}else {
			entity.setPartNo("");
			entity.setPartRevision("");
		}
		
//		private long alternatePartId;
//		private String alternatePartNo;
//		private String alternatePartRevision;

		long alternatePartId = entity.getAlternatePartId();
		Part alternatePart = partService.getById(alternatePartId);
		if(alternatePart == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("alternatePartId "+alternatePartId+" error");
			return resultBean;
		}
		if(flag) {
			entity.setAlternatePartNo(alternatePart.getInnerData().getPartNo());
			entity.setAlternatePartRevision(alternatePart.getInnerData().getRevision());
		}else {
			entity.setPartNo("");
			entity.setPartRevision("");
		}
		
		PartAlternate pa = partAlternateDataHandler.getByPartIdAndAlternateId(partId,alternatePartId);
		if(pa!=null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("the alternate is exist!");
			return resultBean;
		}
		
//		private BigDecimal conversionFactor; //换算系数 1比1；1比2等等
//		private BigDecimal maxReplacementPercent; //替换比例
		BigDecimal conversionFactor = entity.getConversionFactor();
		BigDecimal maxReplacementPercent = entity.getMaxReplacementPercent();
		if(conversionFactor==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("conversionFactor is null");
			return resultBean;
		}
		if(maxReplacementPercent==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("maxReplacementPercent is null");
			return resultBean;
		}
		
//		private long plantId;
		entity.setPlantId(part.getInnerData().getPlantId());
		
//		private String name;
//		private String revision;
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	
	//check update PartAlternate data
	private ResultBean checkUpdatePartAlternateInfo(PartAlternate partAlternate) {
		
		ResultBean resultBean = new ResultBean();
		
		
		PartAlternateEntity entity = partAlternate.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		long id = entity.getId();
		PartAlternate pa = partAlternateDataHandler.getById(id);
		
		if(pa==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partAlternate:"+id+" error");
			return resultBean;
		}

//		private BigDecimal conversionFactor; //换算系数 1比1；1比2等等
//		private BigDecimal maxReplacementPercent; //替换比例
		BigDecimal conversionFactor = entity.getConversionFactor();
		BigDecimal maxReplacementPercent = entity.getMaxReplacementPercent();
		if(conversionFactor==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("conversionFactor is null");
			return resultBean;
		}
		if(maxReplacementPercent==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("maxReplacementPercent is null");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	//check delete PartAlternate data
	private ResultBean checkDeletePartAlternateInfo(PartAlternate partAlternate) {
		ResultBean resultBean = new ResultBean();
		
		PartAlternateEntity entity = partAlternate.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		long id = entity.getId();
		PartAlternate pa = partAlternateDataHandler.getById(id);
		
		if(pa==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partAlternate:"+id+" error");
			return resultBean;
		}

		resultBean.setSuccess(true);
		return resultBean;
	}
}
